/*
 * @author: wizardc
 */

using System;
using System.Collections.Generic;
using UnityEngine;

namespace Dou.ScreenController
{
    /// <summary>
    /// RectTransform 支持横竖屏切换的适配器
    /// 1. 整体上设计为以横竖屏为主，各种语言微调为辅的形式；这是由于横竖屏的情况下布局会出现非常大的变动，而多语言切换后仅少部分布局会改变；
    /// 2. 横屏简中，和竖屏简中，会作为基础数据全部保存，其它语言会基于该基础数据仅保存出现变动的数据；
    /// </summary>
    public class RectTransformAdapter : MonoBehaviour, IUIAdapter
    {
        [HideInInspector]
        public List<RectTransformData> rectTransformDataList = new List<RectTransformData>();
        
        private RectTransform _rectTransform;
        
        void Awake()
        {
            _rectTransform = GetComponent<RectTransform>();
            ScreenUIAdapterMgr.instance.AddAdapter(this);
        }
        
        public void RefreshAdapter(int uiScreenMode, int uiFineTuningMode)
        {
            RectTransformAdapterUtil.CopyFormData(_rectTransform, this, uiScreenMode, uiFineTuningMode);
        }

#if UNITY_EDITOR

        public void RefreshAdapterInEditor(int uiScreenMode, int uiFineTuningMode)
        {
            RectTransformAdapterUtil.CopyFormData(GetComponent<RectTransform>(), this, uiScreenMode, uiFineTuningMode);
        }
        
#endif
        
        void OnDestroy()
        {
            ScreenUIAdapterMgr.instance.RemoveAdapter(this);
        }
    }

    [Serializable]
    public class RectTransformData
    {
        public bool isInit;
        
        public Vector2 anchoredPosition;
        public Vector2 sizeDelta;
        public Vector2 anchorMin;
        public Vector2 anchorMax;
        public Vector2 pivot;
        public Vector3 localEulerAngles;
        public Vector3 localScale;
        
        public List<RectTransformFineTuningData> fineTuningList = new List<RectTransformFineTuningData>();
    }
    
    [Serializable]
    public class RectTransformFineTuningData
    {
        public Vector2? anchoredPosition;
        public Vector2? sizeDelta;
        public Vector2? anchorMin;
        public Vector2? anchorMax;
        public Vector2? pivot;
        public Vector3? localEulerAngles;
        public Vector3? localScale;
    }
}
